import {
  request
} from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  data: {

    phone: "",
    password: "",
    userinfo:{}
  },

  onLoad: function (options) {

  },
  handleInput(e) {
    let type = e.currentTarget.id
    this.setData({
      [type]: e.detail.value
    })
  },
  async login() {
    let {
      phone,
      password
    } = this.data

    //验证手机号
    if (!phone) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      });
      return;
    }

    let phoneReg = /^1(3|4|5|6|7|8|9)\d{9}$/
    if (!phoneReg.test(phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
      });
      return;
    }

    //验证密码
    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
      });
      return;
    }

    let {data} = await request({
      url: "/login/cellphone",
      data: {
        phone,
        password
      }
    })
    // console.log(data);
    if (data.code === 200) {
      wx.showToast({
        title: '登录成功',
      });

      wx.setStorage({
        key: 'cookie',
        data: data.cookie,
      });



      wx.reLaunch({
        url: '/pages/profile/index',
      });

      this.setData({
        userinfo:data.profile
      })

      wx.setStorageSync('userinfo',data.profile);


    } else if (data.code === 400) {
      wx.showToast({
        title: '手机号错误',
        icon: 'none',

      });
    } else if (data.code === 502) {
      wx.showToast({
        title: '密码错误',
        icon: 'none',

      });
    }else{
      wx.showToast({
        title: '登录失败，请重新登录',
        icon: 'none',

      });
    }


  }


})